package yh.core.funcs.workflow.act;

import java.sql.Connection;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import yh.core.data.YHRequestDbConn;
import yh.core.funcs.person.data.YHPerson;
import yh.core.funcs.workflow.util.YHFlowHookUtility;
import yh.core.funcs.workflow.util.YHFlowRunUtility;
import yh.core.funcs.workflow.util.YHWorkFlowUtility;
import yh.core.global.YHActionKeys;
import yh.core.global.YHBeanKeys;
import yh.core.global.YHConst;



public class YHBusinessToFlowAct {
	/**
	 * 设置session
	 * @param map
	 * @param request
	 */
	@SuppressWarnings("unchecked")
	private YHPerson getFlowSession( HttpServletRequest request) {
//		HttpSession session = request.getSession(true);

		YHPerson person=new YHPerson();
		person.setSeqId(Integer.valueOf(request.getParameter("SEQ_ID").toString()));
		person.setAddHome("");
		person.setUserName(String.valueOf(request.getParameter("USER_NAME")));
//		person.setAuthorize(authorize);
		person.setDeptId(Integer.valueOf(request.getParameter("DEPT_ID")));
		person.setUserPriv(request.getParameter("USER_PRIV"));
		person.setUserId(request.getParameter("USER_ID"));
//		session.setAttribute(YHConst.LOGIN_USER,person);
		return person;
	}
	  public String getFlowUrl(HttpServletRequest request,
		      HttpServletResponse response) throws Exception{
//		  long time1=new Date().getTime();
		 
	    	Connection dbConn = null;
			String result = "";
			try {
				// 数据库的连接
				YHRequestDbConn requestDbConn = (YHRequestDbConn) request
				.getAttribute(YHBeanKeys.REQUEST_DB_CONN_MGR);
				dbConn = requestDbConn.getSysDbConn();
				int flowId=Integer.valueOf(request.getParameter("flowId"));
				YHPerson person = this.getFlowSession(request);
				
				YHFlowRunUtility util = new YHFlowRunUtility();
		       int runId = util.createNewWork(dbConn, flowId, person, "", "",request.getParameter("flowBusinessUrl"), null) ;
		        String url= "/core/funcs/workflow/flowrun/list/turn/turnnext.jsp?runId=" + runId + "&flowId=" + flowId + "&prcsId=1&flowPrcs=1";
			if (!"".equals(url)) {
					String path = request.getContextPath();
					result = path + url;
				}
				result = "{\"url\":\"" + result + "\",\"runId\":\""+runId+"\"}";
				request.setAttribute(YHActionKeys.RET_STATE, YHConst.RETURN_OK);
				request.setAttribute("runId", runId);
				request.setAttribute(YHActionKeys.RET_DATA, result);
			} catch (Exception e) {
				request.setAttribute(YHActionKeys.RET_STATE, YHConst.RETURN_ERROR);
				request.setAttribute(YHActionKeys.RET_MSRG, "添加失败");
				throw e;
			}
//			 long time2=new Date().getTime();
//	         System.out.println("2::"+(time2-time1));
			return "/core/inc/rtjson.jsp";
	  }
}
